package LINE.comm.json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.view.AbstractView;

public class JSonResult extends AbstractView{
	
	Logger log = Logger.getLogger(this.getClass());
	
	final String OPEN = "{";
	final String CLOSE = "}";
	final String COMMA = ", ";
	final String COT = "\"";
	final String COL = " : ";
	
	
	public JSonResult() {
		super();
		setContentType("text/x-json; charset=utf-8");
	}

	@Override
	protected void renderMergedOutputModel(Map model,HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		response.setContentType(getContentType());
		StringBuffer result = new StringBuffer(OPEN);
		
		//result.append(setBefor(model));
		
		if(model.get("result") instanceof Map){
			result.append(oneResult(model));
		}else if(model.get("result") instanceof List){
			result.append(ServeralResult(model));
		}else {
			result.append(notResult(model));
		}
		
		result.append(CLOSE);
		//result.append(setAfter(model));
		
		log.debug("============================================================");
		log.debug(result);
		log.debug("============================================================");
		
		response.getWriter().write(result.toString());
	}
	
	private String notResult(Map rs){
		StringBuffer sb = new StringBuffer();
		sb
		.append(COT).append("resultMsg").append(COT).append(COL).append(COT).append(rs.get("resultMsg")).append(COT).append(COMMA)
		.append(COT).append("resultCd").append(COT).append(COL).append(COT).append(rs.get("resultCd")).append(COT).append(COMMA)
		.append(COT).append("traceInfo").append(COT).append(COL).append(COT).append(rs.get("traceInfo")).append(COT).append(COMMA)
		.append(COT).append("errMsg").append(COT).append(COL).append(COT).append(rs.get("errMsg")).append(COT).append(COMMA)
		.append(COT).append("err").append(COT).append(COL).append(COT).append(rs.get("err")).append(COT);
		
		log.debug(sb.toString());
		
		return sb.toString();
	}
	
	private String oneResult(Map rs){
		StringBuffer sb = new StringBuffer();
		Map  o = (Map)rs.get("result");
		
		sb
		.append(COT).append("resultMsg").append(COT).append(COL).append(COT).append(rs.get("resultMsg")).append(COT).append(COMMA)
		.append(COT).append("resultCd").append(COT).append(COL).append(COT).append(rs.get("resultCd")).append(COT).append(COMMA);
		sb.append("\"result\" : ");
	
		if(o != null){
			sb.append(OPEN);
			Set key = o.keySet();
			for (Object object : key) {
				if(!sb.substring(sb.length()-1).equals(OPEN)) {
					sb.append(COMMA);
				}
				sb.append(COT).append(object).append(COT).append(COL).append(COT).append(get(object,o)).append(COT);
			}
			sb.append(CLOSE);
		}
		
		return sb.toString();
	}
	
	private String ServeralResult(Map rs){
		
		List<HashMap> o = (List<HashMap>) rs.get("result");
		StringBuffer sb = new StringBuffer("");
		sb
			.append(COT).append("resultMsg").append(COT).append(COL).append(COT).append(rs.get("resultMsg")).append(COT).append(COMMA)
			.append(COT).append("resultCd").append(COT).append(COL).append(COT).append(rs.get("resultCd")).append(COT).append(COMMA)
		;
		
		sb.append("\"result\" : [");
		
		for (Map map : o) {
			if(sb.substring(sb.length()-1).equals(CLOSE)) {
				sb.append(COMMA);
			}
			sb.append(OPEN);
			Set key = map.keySet();
			for (Object object : key) {
				if(!sb.substring(sb.length()-1).equals(OPEN)) {
					sb.append(COMMA);
				}
				sb.append(COT).append(object).append(COT).append(COL).append(COT).append(get(object,map)).append(COT);
			}
			sb.append(CLOSE);
		}
		sb.append("]").append(CLOSE);
		return sb.toString();
	}
	
	private String setBefor(Map map){
		StringBuffer sb = new StringBuffer("");
		sb
			.append(COT).append("resultMsg").append(COT).append(COL).append(COT).append(map.get("resultMsg")).append(COT).append(COMMA)
			.append(COT).append("resultCd").append(COT).append(COL).append(COT).append(map.get("resultCd")).append(COT).append(COMMA)
		;
		sb.append("\"result\" : [");
		
		return sb.toString();
	}
	
	private String setAfter(Map map){
		StringBuffer sb = new StringBuffer("");
		sb.append(CLOSE);
		sb.append("]").append(CLOSE);
		
		return sb.toString();
	}
	
	
	/**
	 * �����곕�� <br/>濡� 蹂����������.
	 * @param object
	 * @return
	 */
	private Object get(Object key, Map o) {
		Object object = o.get(key);
		
		if(object instanceof java.lang.String) {
			String str = (String)object;
			String result = str.replaceAll("\n", "<br />");
			result = result.replaceAll("\r", "<br/>");
			return result;
		}
		return object;
	}
}
